package com.cskaoyan.service.mallservice;

import com.cskaoyan.bean.mallbean.Brand;
import com.cskaoyan.bean.mallbean.BrandExample;
import com.cskaoyan.bo.mallbo.BrandCreateBo;
import com.cskaoyan.bo.mallbo.BrandUpdateBo;
import com.cskaoyan.mapper.mallmapper.BrandMapper;
import com.cskaoyan.vo.mallvo.BrandCreateVo;
import com.cskaoyan.vo.mallvo.BrandUpdateVo;
import com.cskaoyan.vo.mallvo.BrandVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author hsp
 * @date 2021/5/9 16:40
 */
@Service
public class BrandServiceImpl implements BrandService{
    @Autowired
    BrandMapper brandMapper;
    @Override//admin/brand/list?page=1&limit=20&sort=add_time&order=desc
    public BrandVo queryBrand(Integer page, Integer limit, Integer id,String name,String sort, String order) {
        //分页
        PageHelper.startPage(page,limit);
        //order by add_time desc 条件查询
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        if (id!=null)
            criteria.andIdEqualTo(id);
        if (name!=null)
            criteria.andNameLike("%"+name+"%");
        criteria.andDeletedEqualTo(0);
        brandExample.setOrderByClause(sort +" "+order); //order by add_time desc
        List<Brand> brandList = brandMapper.selectByExample(brandExample);

        //获取total
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);
        long total = brandPageInfo.getSize();

        BrandVo brandVo = new BrandVo();
        brandVo.setItems(brandList);
        brandVo.setTotal(total);

        return brandVo;
    }

    @Override
    public BrandCreateVo createBrand(BrandCreateBo brandCreateBo) {

        Brand brand = new Brand();
        brand.setId(null);
        brand.setName(brandCreateBo.getName());
        brand.setDesc(brandCreateBo.getDesc());
        brand.setPicUrl(brandCreateBo.getPicUrl());
        brand.setSortOrder(Byte.valueOf("88"));
        brand.setFloorPrice(BigDecimal.valueOf(Double.parseDouble(brandCreateBo.getFloorPrice())));
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setDeleted(false);

        brandMapper.insert(brand);

        BrandCreateVo brandCreateVo = new BrandCreateVo();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTime = simpleDateFormat.format(brand.getAddTime());
        brandCreateVo.setAddTime(addTime);
        brandCreateVo.setDesc(brand.getDesc());
        brandCreateVo.setFloorPrice(brand.getFloorPrice().intValue());
        brandCreateVo.setId(brand.getId());
        brandCreateVo.setName(brand.getName());
        brandCreateVo.setPicUrl(brand.getPicUrl());
        brandCreateVo.setUpdateTime(simpleDateFormat.format(brand.getUpdateTime()));
        return brandCreateVo;
    }

    @Override
    public BrandUpdateVo updateBrand(BrandUpdateBo brandUpdateBo)  {

        Brand brand = new Brand();
        brand.setId(brandUpdateBo.getId());
        brand.setName(brandUpdateBo.getName());
        brand.setDesc(brandUpdateBo.getDesc());
        brand.setPicUrl(brandUpdateBo.getPicUrl());
//        brand.setSortOrder();
        brand.setFloorPrice( BigDecimal.valueOf(brandUpdateBo.getFloorPrice()));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            brand.setAddTime(simpleDateFormat.parse(brandUpdateBo.getAddTime()));
            Date updateTime = simpleDateFormat.parse(brandUpdateBo.getUpdateTime());
            brand.setUpdateTime(updateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        brandMapper.updateByPrimaryKeySelective(brand);
        Brand brand2 = brandMapper.selectByPrimaryKey(brand.getId());
        BrandUpdateVo brandUpdateVo = new BrandUpdateVo(brand2.getId(),
                brand2.getName(),
                brand2.getDesc(),
                brand2.getPicUrl(),
                brand2.getSortOrder().intValue(),
                brand2.getFloorPrice().intValue(),
                simpleDateFormat.format(brand2.getAddTime()),
                simpleDateFormat.format(brand2.getUpdateTime()),
                brand2.getDeleted()
                );


        return brandUpdateVo;
    }

    @Override
    public void deleteBrandById(int id) {

        brandMapper.logicDeleteBrandById(id);


    }
}
